package rd.system.toeicbridge {
	
	import com.greensock.TweenLite;
	import com.sff.core.ILoadable;
	import com.sff.manager.LoaderManager;
	import com.sff.module.Module;
	import com.sff.module.ModuleLoader;
	import com.sff.net.LoaderItem;
	import com.sff.SFFCore;
	import com.sff.site.core.INode;
	import com.sff.site.events.NodeEvent;
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.external.ExternalInterface;
	import flash.net.URLRequest;
	
	/**
	 * ...
	 * @author cuong.tran@rdsystem.org
	 */
	public class ToeicBridge extends Module {
		
		public var mcMenu:MovieClip;
		public var mcHolder: MovieClip;
		
		public function ToeicBridge() {				
				
			
		}
		
		override protected function run():void {			
			
		}
		
		override public function getLoadables():ILoadable {
			
			var loaderMgr:LoaderManager = new LoaderManager();
			loaderMgr.addEventListener(Event.COMPLETE, onCompleteLoadableHandler);
			
			var xmlMenu:XMLList = this.system.dataManager.xmlData.menus.menu;
			var ldr:LoaderItem;
			var menuItem:MenuItem;
			var ypos:Number = 0;
			
			for (var i:int = 0; i < xmlMenu.length(); i ++) {
				menuItem = new MenuItem_FC();
				menuItem.name = xmlMenu[i].@nodeid.toString();
				menuItem.setData(this.system, xmlMenu[i]);
				menuItem.y = ypos;
				
				mcMenu.addChild(menuItem);
				ypos += menuItem.height + 5;
				
				ldr = new LoaderItem(new URLRequest(xmlMenu[i].image.toString()));
				ldr.loader.name = menuItem.name;
				ldr.addEventListener(Event.COMPLETE, onCompleteLoadMenuImageHandler);
				
				loaderMgr.addItem(ldr);
			}
			
			return loaderMgr;
		}
		
		
		private function onCompleteLoadMenuImageHandler(evt:Event):void {
			evt.currentTarget.removeEventListener(Event.COMPLETE, onCompleteLoadMenuImageHandler);
			
			var menuItem:MenuItem = mcMenu.getChildByName(evt.currentTarget.loader.name) as MenuItem;
			
			var bmp:Bitmap = evt.currentTarget.loader.content as Bitmap;
			bmp.smoothing = true;
			menuItem.addImage(bmp);
		}
		
		private function onCompleteLoadableHandler(evt:Event):void {				
			evt.currentTarget.removeEventListener(Event.COMPLETE, onCompleteLoadableHandler);
			
			this.system.siteManager.addEventListener(NodeEvent.CHILD_CHANGE, onChildChangeHandler);
			onChildChangeHandler(null);
		}
		
		private function onChildChangeHandler(evt:NodeEvent):void {
			
			var activeNode:INode = this.system.siteManager.getCurrentChild();
			activeNode = activeNode ? activeNode : this.system.siteManager.getDefaultNode();
			
			if (activeNode) {
				loadModule(activeNode.getId());				
			}			
		}
		
		private function removeModules():void {
			var module:Module;
			while (mcHolder.numChildren) {
				module = mcHolder.removeChildAt(0) as Module;
				
				if (module) {
					module.cleanUp();
					module = null;
				}
			}
		}
		
		private function loadModule(nodeId:String):void {
			removeModules();			
			
			var url:String = this.system.dataManager.xmlData.menus.menu.(@nodeid == nodeId).url.toString();
			var ldr:ModuleLoader = this.system.getModuleLoader();
			ldr.addEventListener(Event.COMPLETE, onCompleteLoadModuleHandler);
			ldr.load(new URLRequest(url), this.system.dataManager);
		}
		
		private function onCompleteLoadModuleHandler(evt:Event):void {
			removeModules();
			
			mcHolder.addChild(evt.currentTarget.module);
		}
	}
}